package com.kafka.valid;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.kafka.bean.Iccard;
import com.kafka.common.Constant;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseClass;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.CheckUtil;

public class PayValid extends BaseClass{
	/**
	 * 校验消费
	 * @param goodsObj
	 * @param result
	 * @param msgId
	 * @return
	 */
	public static JSONObject validPay(JSONArray goodsObj,
			String msgId) {
		JSONObject result = new JSONObject();
		//消费传递的是数据集，需要遍历校验所有消费数据
		for (int i = 0; i < goodsObj.size(); i++) {
			JSONObject obj = JSONObject.fromObject(goodsObj.get(i));
			try {
				//**************************必填字段校验*****************************************
				if(obj.getString("uuid").isEmpty()){
					return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
			
				if(obj.getString("sfrom").isEmpty()){
					return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("cardtype").isEmpty()){
					return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("realcharges").isEmpty()){
					return failMsg(result,StringValues.AMOUNT_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("product").isEmpty()){
					return failMsg(result,StringValues.SHOPCODE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("ptype").isEmpty()){
					return failMsg(result,StringValues.GOODSTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("shopcode").isEmpty()){
					return failMsg(result,StringValues.SHOPCODE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("paydevice").isEmpty()){
					return failMsg(result,StringValues.PAYDEVICE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(obj.getString("stime").isEmpty()){
					return failMsg(result,StringValues.PAYDDATE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				//**************************查询字段校验*****************************************
				Map<Object, Object> query = new HashMap<>();
				query.put("iccardno", obj.getString("sfrom"));
				Iccard iccard = iccardDao.selectByCondition(query);
				if (iccard == null) {
					return failMsg(result,StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString(),msgId);
				}
				
				if(!iccard.getUuid().equals(obj.getString("uuid"))){
					return failMsg(result,StringValues.NO_MATCH_UUID,ResponseType.UUID_NOT_MATCH.toString(),msgId);
				}
				
				if(iccard!=null && iccard.getConsumeCheck()==Constant.MUSTCHECK){
					if(!iccard.getPassword().equals(obj.getString("password"))){
						return failMsg(result,StringValues.PAYPASSWD_ERROR,ResponseType.PAY_PASSWORD_ERROR.toString(),msgId);
					}
				}
				
				//**************************数据格式校验*****************************************
				if(!CheckUtil.isNumber(obj.getString("scount")) && !obj.getString("scount").isEmpty()){
					return failMsg(result,StringValues.GOODSCOUNT_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("id")) && !obj.getString("id").isEmpty()){
					return failMsg(result,StringValues.ID_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("sprice")) && !obj.getString("sprice").isEmpty()){
					return failMsg(result,StringValues.SPRICE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("discountprice")) && !obj.getString("discountprice").isEmpty()){
					return failMsg(result,StringValues.DISCOUNTPRICE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("cardtype")) && !obj.getString("cardtype").isEmpty()){
					return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("shopcode")) && !obj.getString("shopcode").isEmpty()){
					return failMsg(result,StringValues.SHOPCODE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("discount")) && !obj.getString("discount").isEmpty()){
					return failMsg(result,StringValues.DISCOUNT_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("realcharges")) && !obj.getString("realcharges").isEmpty()){
					return failMsg(result,StringValues.AMOUNT_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("costprice")) && !obj.getString("costprice").isEmpty()){
					return failMsg(result,StringValues.COSTPRICE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isNumber(obj.getString("pid")) && !obj.getString("pid").isEmpty()){
					return failMsg(result,StringValues.PID_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
				if(!CheckUtil.isDateTime(obj.getString("stime")) && !obj.getString("stime").isEmpty()){
					return failMsg(result,StringValues.PAYDDATE_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
			}
		}
		return null;
	}
	
	/**
	 * 校验充值
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validRecharge(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		
			if(params.getString("cardno").isEmpty()){
				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("amount").isEmpty()){
				return failMsg(result,StringValues.AMOUNT_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
//			if(params.getString("goodsname").isEmpty()){
//				return failMsg(result,StringValues.GOODSNAME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
//			}
//			
//			if(params.getString("goodstype").isEmpty()){
//				return failMsg(result,StringValues.GOODSTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
//			}
			
			if(params.getString("shopcode").isEmpty()){
				return failMsg(result,StringValues.SHOPCODE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("paydevice").isEmpty()){
				return failMsg(result,StringValues.PAYDEVICE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("paydate").isEmpty()){
				return failMsg(result,StringValues.PAYDDATE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("payno").isEmpty()){
				return failMsg(result,StringValues.PAYNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_EXIST.toString(),msgId);
			}
			
			query.clear();
			query.put("iccardno", params.getString("cardno"));
			query.put("card_type", params.getString("cardtype"));
			Iccard iccard = iccardDao.selectByCondition(query);
			if (iccard == null) {
				return failMsg(result,StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString(),msgId);
			}
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("cardtype")) && !params.getString("cardtype").isEmpty()){
				return failMsg(result,StringValues.CARDTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("amount")) && !params.getString("amount").isEmpty()){
				return failMsg(result,StringValues.AMOUNT_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isRechargeDate(params.getString("paydate")) && !params.getString("paydate").isEmpty()){
				return failMsg(result,StringValues.PAYDDATE_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
	}
}
